| Conditions | 1 |
| Paths | 2 |
| Total Lines | 216 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | 'use strict'; |
||
| 7 | function(state, data) { |
||
| 8 | let newElements = []; |
||
| 9 | |||
| 10 | function visible(items, func, currentElement) { |
||
| 11 | let visibles = []; |
||
| 12 | for (let i in items) { |
||
| 13 | // if it is an array, we need to extract the item from the index |
||
| 14 | let item = Array.isArray(items) ? items[i] : i; |
||
| 15 | if (func(item, currentElement)) { |
||
| 16 | visibles.push(item); |
||
| 17 | } |
||
| 18 | } |
||
| 19 | return visibles; |
||
| 20 | } |
||
| 21 | |||
| 22 | this.visibleElements = function() { |
||
| 23 | return visible(data.elements, isElementVisible); |
||
| 24 | }; |
||
| 25 | |||
| 26 | this.visibleGenerators = function(currentElement) { |
||
| 27 | return visible(data.generators, isGeneratorVisible, currentElement); |
||
| 28 | }; |
||
| 29 | |||
| 30 | this.visibleUpgrades = function(currentElement) { |
||
| 31 | return visible(data.upgrades, isBasicUpgradeVisible, currentElement); |
||
| 32 | }; |
||
| 33 | |||
| 34 | this.visibleExoticUpgrades = function(currentElement) { |
||
| 35 | return visible(data.exotic_upgrades, isExoticUpgradeVisible, currentElement); |
||
| 36 | }; |
||
| 37 | |||
| 38 | this.visibleDarkUpgrades = function(currentElement) { |
||
| 39 | return visible(data.dark_upgrades, isDarkUpgradeVisible, currentElement); |
||
| 40 | }; |
||
| 41 | |||
| 42 | this.visibleResources = function(currentElement, type) { |
||
| 43 | let resources = visible(data.resources, isResourceVisible, currentElement); |
||
| 44 | if(type){ |
||
| 45 | let filteredResources = []; |
||
| 46 | for(let resource of resources){ |
||
| 47 | if(data.resources[resource].type && |
||
| 48 | data.resources[resource].type.indexOf(type) !== -1){ |
||
| 49 | filteredResources.push(resource); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | return filteredResources; |
||
| 53 | } |
||
| 54 | return resources; |
||
| 55 | }; |
||
| 56 | |||
| 57 | this.visibleEncyclopediaEntries = function() { |
||
| 58 | return visible(data.encyclopedia, isEncyclopediaEntryVisible); |
||
| 59 | }; |
||
| 60 | |||
| 61 | this.visibleRedox = function(currentElement) { |
||
| 62 | return visible(state.player.redox, isRedoxVisible, currentElement); |
||
| 63 | }; |
||
| 64 | |||
| 65 | this.visibleSyntheses = function(currentElement) { |
||
| 66 | return visible(data.reactions, isSynthesisVisible, currentElement); |
||
| 67 | }; |
||
| 68 | |||
| 69 | function isElementVisible(element) { |
||
| 70 | if (data.elements[element].disabled) { |
||
| 71 | return false; |
||
| 72 | } |
||
| 73 | |||
| 74 | for (let resource of data.elements[element].includes) { |
||
| 75 | if (state.player.resources[resource].unlocked) { |
||
| 76 | return true; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | return false; |
||
| 81 | } |
||
| 82 | |||
| 83 | function isGeneratorVisible(name, currentElement) { |
||
| 84 | let generator = data.generators[name]; |
||
| 85 | for (let dep of generator.deps) { |
||
| 86 | if (state.player.elements[currentElement].generators[dep] === 0) { |
||
| 87 | return false; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | return true; |
||
| 92 | } |
||
| 93 | |||
| 94 | function isBasicUpgradeVisible(name, currentElement) { |
||
| 95 | return isUpgradeVisible(name, currentElement, data.upgrades[name]); |
||
| 96 | } |
||
| 97 | |||
| 98 | function isExoticUpgradeVisible(name, currentElement) { |
||
| 99 | return isUpgradeVisible(name, currentElement, data.exotic_upgrades[name]); |
||
| 100 | } |
||
| 101 | |||
| 102 | function isDarkUpgradeVisible(name, currentElement) { |
||
| 103 | return isUpgradeVisible(name, currentElement, data.dark_upgrades[name]); |
||
| 104 | } |
||
| 105 | |||
| 106 | function meetDependencies(upgrades, dependencies) { |
||
| 107 | if (!dependencies) { |
||
| 108 | return true; |
||
| 109 | } |
||
| 110 | for (let dep of dependencies) { |
||
| 111 | if (!upgrades[dep]) { |
||
| 112 | return false; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | return true; |
||
| 116 | } |
||
| 117 | |||
| 118 | function isUpgradeVisible(name, currentElement, upgrade) { |
||
| 119 | if (upgrade.tiers) { |
||
| 120 | for (let tier of upgrade.tiers) { |
||
| 121 | if (state.player.elements[currentElement].generators[tier] === 0) { |
||
| 122 | return false; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | return meetDependencies(state.player.elements[currentElement].upgrades, upgrade.deps) && |
||
| 127 | meetDependencies(state.player.elements[currentElement].exotic_upgrades, upgrade.exotic_deps) && |
||
| 128 | meetDependencies(state.player.dark_upgrades, upgrade.dark_deps); |
||
| 129 | } |
||
| 130 | |||
| 131 | function isResourceVisible(name, currentElement) { |
||
| 132 | if (!state.player.resources[name].unlocked) { |
||
| 133 | return false; |
||
| 134 | } |
||
| 135 | |||
| 136 | // This is for global resources e.g. protons, which do not |
||
| 137 | // belong to any element |
||
| 138 | let elements = data.resources[name].elements; |
||
| 139 | if (Object.keys(elements).length === 0) { |
||
| 140 | return true; |
||
| 141 | } |
||
| 142 | |||
| 143 | for (let element in elements) { |
||
| 144 | if (currentElement === element) { |
||
| 145 | return true; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | return false; |
||
| 150 | } |
||
| 151 | |||
| 152 | function isEncyclopediaEntryVisible(entry) { |
||
| 153 | return state.player.achievements[entry]; |
||
| 154 | } |
||
| 155 | |||
| 156 | function isReactionVisible(entry, currentElement, reaction) { |
||
| 157 | if (!state.player.achievements[reaction]) { |
||
| 158 | return false; |
||
| 159 | } |
||
| 160 | |||
| 161 | for (let reactant in entry.reactant) { |
||
| 162 | if (!state.player.resources[reactant].unlocked) { |
||
| 163 | return false; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | // for misc reactions |
||
| 168 | if(entry.elements.length === 0 && |
||
| 169 | currentElement === ''){ |
||
| 170 | return true; |
||
| 171 | } |
||
| 172 | |||
| 173 | for (let element in entry.elements) { |
||
| 174 | if (currentElement === entry.elements[element]) { |
||
| 175 | return true; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | return false; |
||
| 180 | } |
||
| 181 | |||
| 182 | function isRedoxVisible(entry, currentElement) { |
||
| 183 | return entry.element === currentElement; |
||
| 184 | } |
||
| 185 | |||
| 186 | function isSynthesisVisible(entry, currentElement) { |
||
| 187 | return isReactionVisible(data.reactions[entry], currentElement, 'reaction'); |
||
| 188 | } |
||
| 189 | |||
| 190 | this.elementHasNew = function(element) { |
||
| 191 | let includes = data.elements[element].includes; |
||
| 192 | for (let key in includes) { |
||
| 193 | if (this.hasNew(includes[key])) { |
||
| 194 | return true; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | return false; |
||
| 198 | }; |
||
| 199 | |||
| 200 | this.encyclopediaHasNew = function() { |
||
| 201 | for (let entry in data.encyclopedia) { |
||
| 202 | if (this.hasNew(entry)) { |
||
| 203 | return true; |
||
| 204 | } |
||
| 205 | } |
||
| 206 | return false; |
||
| 207 | }; |
||
| 208 | |||
| 209 | this.hasNew = function(entry) { |
||
| 210 | return newElements.indexOf(entry) !== -1; |
||
| 211 | }; |
||
| 212 | |||
| 213 | this.addNew = function(entry) { |
||
| 214 | newElements.push(entry); |
||
| 215 | }; |
||
| 216 | |||
| 217 | this.removeNew = function(entry) { |
||
| 218 | if (newElements.indexOf(entry) !== -1) { |
||
| 219 | newElements.splice(newElements.indexOf(entry), 1); |
||
| 220 | } |
||
| 221 | }; |
||
| 222 | } |
||
| 223 | ]); |
||
| 224 |